package it.greg.findparty.util;

import it.greg.findparty.android.util.GestoreErrori;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class HtmlUtil {
	
	private HttpClient Client;
	private GestoreErrori errManager;
	
	public HtmlUtil(GestoreErrori e){
		this();
		this.errManager = e;
	}
	
	public HtmlUtil() {
		this.Client = new DefaultHttpClient();
		this.errManager = null;
	}

	public void setGestoreErrori(GestoreErrori e){
		this.errManager = e;
	}
	
	/* Ottiene il codice html dalla pagina specificata nella variabile URL */
	public String getHTML(String URL) {
		// Use URLEncoder.encode to encode parameters
		try {
			String SetServerString = "";
			HttpGet httpget = new HttpGet(URL);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			SetServerString = Client.execute(httpget, responseHandler);
			// Show response on activity
			return SetServerString;
		} catch (Exception e) {
			//e.printStackTrace();
			if (this.errManager!=null){
				this.errManager.showError("Errore fetching html:\n"+e.getMessage());
			}
			return null;
		}
	}
	
}
